import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Activity from '@/views/Activity.vue'
import Study from '@/views/Study.vue'
import Declare from '@/views/Declare.vue'
import Mine from '@/views/Mine.vue'
import { useUserStore } from '@/stores/userStore'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        keepAlive: true // 新增缓存标记
      }
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue'),
    },
    {
      path: '/study',
      name: 'study',
      component: Study,
      meta: {
        keepAlive: false // 新增缓存标记
      }
    },
    {
      path: '/activity',
      name: 'activity',
      component: Activity,
    },
    {
      path: '/declare',
      name: 'declare',
      component: Declare,
    },
    {
      path: '/mine',
      name: 'mine',
      component: Mine,
    },
    {
      path: '/progress',
      name: 'progress',
      component: () => import('../views/progress.vue'),/* 申报进度 */
    },
    {
      path: '/mycase',
      name: 'mycase',
      component: () => import('../views/mycase.vue'),/* 我的申报 */
    },    {
      path: '/deal',
      name: 'deal',
      component: () => import('../views/deal.vue'),/* 阿里处理 */
    }, {
      path: '/flowpath',
      name: 'flowpath',
      component: () => import('../views/flowpath.vue'),/* 申报流程 */
    }, {
      path: '/compare',
      name: 'compare',
      component: () => import('../views/compare.vue'),/* 对比 */
    },  {
      path: '/add',
      name: 'add',
      component: () => import('../views/add.vue'),/* 申报 */
    }, 
    {
      path: '/study/detail/:id',
      name: 'studydetail',
      component: () => import('../views/studydetail.vue'),/* 学习详情 */
    },
    {
      path: '/homeviewtwo/:id',
      name: 'homeviewtwo',
      component: () => import('../views/HomeViewTwo.vue'),
    },
    {
      path: '/personhomepage',
      name: 'personhomepage',
      component: () => import('../views/PersonalHomepage.vue'),
      meta: {
        requiresAuth: true, // 添加 requiresAuth 属性，表示需要登录才能访问
      },
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginForm.vue') // 推荐使用懒加载
    },

    {
      path: '/aboutme',
      name: 'aboutme',
      component: () => import('@/views/AboutMe.vue'),
    },
    //feedback
    {
      path: '/feedback',
      name: 'feedback',
      component: () => import('@/views/Feedback.vue'),
    },
    //set
    {
      path: '/set',
      name: 'set',
      component: () => import('@/views/Set.vue'),
    },
    //help
    {
      path: '/help',
      name: 'help',
      component: () => import('@/views/Help.vue'),
    },
    //download
    {
      path: '/download',
      name: 'download',
      component: () => import('@/views/Download.vue'),
    },
    //zhengshu
    {
      path: '/zhengshu',
      name: 'zhengshu',
      component: () => import('@/views/Zhengshu.vue'),
    },
    //topic
    {
      path: '/topic',
      name: 'topic',
      component: () => import('@/views/Topic.vue'),
    },
    {
      path: '/item/:id',
      name: 'Item',
      component: () => import('../views/Itemview.vue')
    },
    //Register
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/Register.vue'),
    },
  ],
  
})



export default router
// 添加导航守卫（用于登录验证）
router.beforeEach((to, from) => {
  const store = useUserStore() // 从Pinia获取用户状态
  
  // 需要登录但未登录时跳转到登录页
  if (to.meta.requiresAuth && !store.isAuthenticated) {
    return {
      path: '/login',
      query: { redirect: to.fullPath }
    }
  }
})